package zhishu.iceyyy.com.bxq_2500.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import zhishu.iceyyy.com.bxq_2500.R;


/**
 * ---------------------------------------------------------------
 * Author: LiYi
 * Create: 16-3-8 14:05
 * ---------------------------------------------------------------
 * Describe:
 * ---------------------------------------------------------------
 * Changes:
 * ---------------------------------------------------------------
 * 16-3-8 14 : Create by LiYi
 * ---------------------------------------------------------------
 */
public class MenuDialog<T> extends BaseBottomAnimDialog {

    private Context mContext;
    /**
     * 标题
     */
    private String mTitle;
    /**
     * menu数据
     */
    private List<T> mData;


    /**
     * 是否展示Menu
     */
    private boolean isShowMenu;


    /**
     * 是否第一次加载
     */
    private boolean firstShow = true;

    /**
     * @param context
     * @param title         操作类型提示,如果为空则不显示
     * @param isShowMenu    是否显示menu键（如手机为虚拟键盘，则在虚拟键盘位置显示menu键）
     * @param goneWindowDim 隐藏dialog window 的背景
     * @param
     */
    private MenuDialog(Context context, Serializable title, boolean isShowMenu,
                       boolean goneWindowDim) {
        super(context, goneWindowDim);
        this.mContext = context;
        if (title instanceof Integer) {
            Integer titleId = (Integer) title;
            this.mTitle = mContext.getResources().getString(titleId);
        } else if (title instanceof String) {
            this.mTitle = (String) title;
        }

        this.isShowMenu = isShowMenu;

    }

    String name, bieming, daoyan, zhuyan, diqu, gengxinshijian, nianfen, pingfen,
            juqingjieshao;

    /**
     * @param context
     * @param title         操作类型提示,如果为空则不显示
     * @param data          数据源
     * @param isShowMenu    是否显示menu键（如手机为虚拟键盘，则在虚拟键盘位置显示menu键）
     * @param goneWindowDim 隐藏dialog window 的背景
     * @param
     */
    public MenuDialog(Context context, Serializable title, List<T> data,
                      boolean isShowMenu, boolean goneWindowDim,
                      String name, String bieming, String daoyan, String zhuyan, String diqu, String gengxinshijian, String nianfen, String pingfen,
                      String juqingjieshao) {
        this(context, title, isShowMenu, goneWindowDim);
        this.mData = data;
        this.name = name;
        this.bieming = bieming;
        this.daoyan = daoyan;
        this.zhuyan = zhuyan;
        this.diqu = diqu;
        this.gengxinshijian = gengxinshijian;
        this.juqingjieshao = juqingjieshao;

        this.pingfen = pingfen;
        this.nianfen = nianfen;

    }

    TextView tv_title, tv_bieming, tv_daoyan, tv_zhuyan, tv_diqu, tv_gengxinshijian, tv_nianfen, tv_pingfen, tv_juqing;


    @Override
    public int getLayoutRes() {
        return R.layout.menu_dialog_layout;
    }

    @Override
    public void configView(View rootView) {
        if (isShowMenu) {
            //虚拟键盘显示menu按键
            try {
                getWindow().addFlags(
                        WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            setOnKeyListener(keyListener);
        }
        tv_title = (TextView) rootView.findViewById(R.id.tv_title);
        tv_bieming = (TextView) rootView.findViewById(R.id.tv_bieming);
        tv_daoyan = (TextView) rootView.findViewById(R.id.tv_daoyan);
        tv_zhuyan = (TextView) rootView.findViewById(R.id.tv_zhuyan);
        tv_diqu = (TextView) rootView.findViewById(R.id.tv_diqu);
        tv_gengxinshijian = (TextView) rootView.findViewById(R.id.tv_gengxinshijian);
        tv_nianfen = (TextView) rootView.findViewById(R.id.tv_nianfen);
        tv_pingfen = (TextView) rootView.findViewById(R.id.tv_pingfen);
        tv_juqing = (TextView) rootView.findViewById(R.id.tv_jieshao);
        ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        tv_title.setText("" + name);
        tv_bieming.setText("别名:" + bieming);
        tv_daoyan.setText("导演:" + daoyan);
        tv_zhuyan.setText("主演:" + zhuyan);
        tv_diqu.setText("地区:" + diqu);
        tv_gengxinshijian.setText("更新时间:" + gengxinshijian);
        tv_nianfen.setText("年份:" + nianfen);
        tv_pingfen.setText("评分:" + pingfen);
        tv_juqing.setText(juqingjieshao);


        //TODO：如果外部通过构造方法传入自定义的adapter，则使用外部传入的adapter，否则创建自己的adapter


    }


    @Override
    public void show() {
        super.show();
    }

    /**
     * Menu按键监听
     */
    private OnKeyListener keyListener = new OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent event) {
            Log.d("liyi", "onkey event: " + event.toString());
            //这里为何监听？因为dialog展示出来后Activity的监听就失效了。
            //同时这里只监听ACTION_UP是因为Activity中监听了OnKeyDown，UP事件会被传递过来。
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_UP) {
                if (firstShow) {
                    firstShow = false;
                    return true;
                }
                dismiss();
                return true;
            }
            return false;
        }
    };
}
